package com.cxyzy.tools.cq

import java.util.LinkedList

class CountQueue<T> {
    private val queue = LinkedList<CountQueueItem<T>>()

    fun add(item: T, count: Int) {
        if (count > 0) {
            queue.add(CountQueueItem(item, count))
        }
    }

    fun next(): T? {
        val countQueueItem = queue.peek()
        return if (countQueueItem != null) {
            if (countQueueItem.count <= 1) {
                queue.remove(countQueueItem)
            }
            countQueueItem.reduceCount()
            countQueueItem.item
        } else {
            null
        }
    }
}
